package com.rundatop.wx.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.apache.commons.lang3.StringUtils;
import com.rundatop.wx.service.IService;

import tk.mybatis.mapper.common.Mapper;

public abstract class BaseService<T> implements IService<T> {

    @Autowired
    protected Mapper<T> mapper;

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    public Mapper<T> getMapper() {
        return mapper;
    }

    @Override
    public T selectByKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    @Override
    public int save(T entity) {
        return mapper.insertSelective(entity);
    }

    @Override
    public int delete(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    @Override
    public int updateAll(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    @Override
    public int updateNotNull(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    public int countByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    public boolean isBlank(CharSequence src) {
        return StringUtils.isBlank(src);
    }

    public boolean isBlank(String[] src) {
        return src == null || src.length == 0;
    }

    public boolean isBlank(Object src) {
        return src == null;
    }

    public boolean isBlank(Collection<?> src) {
        return src == null || src.size() == 0;
    }

    public boolean isNotBlank(CharSequence src) {
        return StringUtils.isNotBlank(src);
    }

    public boolean isNotBlank(Object src) {
        return src != null;
    }

    public boolean isNotBlank(String[] src) {
        return !this.isBlank(src);
    }

    public boolean isNotBlank(Collection<?> src) {
        return !this.isBlank(src);
    }

    protected String leftLike(String cond) {
        return new StringBuilder().append("%").append(StringUtils.stripToEmpty(cond)).toString();
    }

    protected String fullLike(String cond) {
        return new StringBuilder().append("%").append(StringUtils.stripToEmpty(cond)).append("%").toString();
    }

    protected String rightLike(String cond) {
        return new StringBuilder().append(StringUtils.stripToEmpty(cond)).append("%").toString();
    }
    
    protected String getUUID() {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replaceAll("-", "");
        return uuid.toUpperCase();
    }

}
